/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TUFormDataSiswa.java
 *
 * Created on Nov 9, 2012, 2:44:15 PM
 */
package Form;

import Boundary.*;
import Control.*;
import Table.*;
import java.sql.SQLException;
import java.util.*;
import java.util.logging.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Fave
 */
public class TUFormDataSiswa extends javax.swing.JFrame {

    /** Creates new form TUFormDataSiswa */
    public TUFormDataSiswa() {
        setResizable(false);
         setTitle("Sistem Presensi Akademik SMS Gateway");
        try {
            initComponents();
            List<TU> tu = (List<TU>) TUkontrol.getKoneksi().getAll();
            TUDataSiswaTabelModel model = new TUDataSiswaTabelModel(tu);
            TabelSiswa.setModel(model);
              refresh();
        } catch (Exception e) {
            Logger.getLogger(TUFormDataSiswa.class.getName()).log(Level.SEVERE, null, e);
        }
      
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jPopupMenu1 = new javax.swing.JPopupMenu();
        Delete = new javax.swing.JMenuItem();
        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TabelSiswa = new javax.swing.JTable();
        HapusButton = new javax.swing.JButton();
        UbahButton = new javax.swing.JButton();
        KeluarButton = new javax.swing.JButton();
        TambahButton = new javax.swing.JButton();
        refreshbutton = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        Delete.setText("Delete");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });
        jPopupMenu1.add(Delete);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/banner1.jpg"))); // NOI18N

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Data Siswa", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14))); // NOI18N

        TabelSiswa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "NIS", "NAMA", "KELAS", "ALAMAT", "JENIS KELAMIN"
            }
        ));
        TabelSiswa.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                TabelSiswaMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(TabelSiswa);

        HapusButton.setText("Hapus");
        HapusButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HapusButtonActionPerformed(evt);
            }
        });

        UbahButton.setText("Update");
        UbahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UbahButtonActionPerformed(evt);
            }
        });

        KeluarButton.setText("Keluar");
        KeluarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                KeluarButtonActionPerformed(evt);
            }
        });

        TambahButton.setText("Tambah");
        TambahButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TambahButtonActionPerformed(evt);
            }
        });

        refreshbutton.setText("Refresh Tabel");
        refreshbutton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                refreshbuttonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(201, 201, 201)
                        .addComponent(refreshbutton)
                        .addGap(26, 26, 26)
                        .addComponent(TambahButton)
                        .addGap(14, 14, 14)
                        .addComponent(HapusButton, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(UbahButton, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 131, Short.MAX_VALUE)
                        .addComponent(KeluarButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 768, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(34, 34, 34)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 142, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(31, 31, 31)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(TambahButton)
                    .addComponent(KeluarButton)
                    .addComponent(HapusButton)
                    .addComponent(UbahButton)
                    .addComponent(refreshbutton))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 271, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void UbahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UbahButtonActionPerformed
        // TODO add your handling code here:
        EditDataSiswa eds = new EditDataSiswa(this, rootPaneCheckingEnabled);
        eds.setVisible(true);
    }//GEN-LAST:event_UbahButtonActionPerformed

    private void KeluarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_KeluarButtonActionPerformed
        // TODO add your handling code here:
        TUForm tf = new TUForm();
        tf.setVisible(true);
        dispose();
    }//GEN-LAST:event_KeluarButtonActionPerformed

    private void TambahButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TambahButtonActionPerformed
        // TODO add your handling code here:
        SaveDataSiswa save = new SaveDataSiswa(this, rootPaneCheckingEnabled);
        save.setVisible(true);
    }//GEN-LAST:event_TambahButtonActionPerformed

    private void TabelSiswaMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TabelSiswaMouseReleased
        // TODO add your handling code here:
        int row = TabelSiswa.rowAtPoint(evt.getPoint());
        TabelSiswa.getSelectionModel().setSelectionInterval(row, row);

        if (evt.isPopupTrigger()) {
            int xc = evt.getX();
            int yc = evt.getY();
            jPopupMenu1.show(evt.getComponent(), evt.getX(), evt.getY());
    }//GEN-LAST:event_TabelSiswaMouseReleased
    }
        private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
            // TODO add your handling code here:
            int row = TabelSiswa.getSelectedRow();
            String nis = TabelSiswa.getValueAt(row, 0).toString();
            siswa s = new siswa();
            s.setNIS(nis);
            TU tu = new TU();
            tu.setNis(s);

            try {
                TUkontrol tukontrol;
                tukontrol = TUkontrol.getKoneksi();
                tukontrol.delete(tu);

                JOptionPane.showMessageDialog(this, "Data telah dihapus");
                refresh();
                //reset();
            } catch (SQLException ex) {
                Logger.getLogger(TUFormDataSiswa.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Data Siswa Belum Dihapus");
            }
            refresh();


        }//GEN-LAST:event_DeleteActionPerformed

        private void HapusButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HapusButtonActionPerformed
            // TODO add your handling code here:
            int row = TabelSiswa.getSelectedRow();
            String nis = TabelSiswa.getValueAt(row, 0).toString();


            siswa s = new siswa();
            s.setNIS(nis);
            TU tu = new TU();
            tu.setNis(s);

            try {
                TUkontrol tukontrol;
                tukontrol = TUkontrol.getKoneksi();
                tukontrol.delete(tu);

                JOptionPane.showMessageDialog(this, "Data telah dihapus");
                refresh();
                //reset();
            } catch (SQLException ex) {
                Logger.getLogger(TUFormDataSiswa.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this, "Data Siswa Belum Dihapus");

            }

            refresh();

        }//GEN-LAST:event_HapusButtonActionPerformed

        private void refreshbuttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_refreshbuttonActionPerformed
            // TODO add your handling code here:
            refresh();
        }//GEN-LAST:event_refreshbuttonActionPerformed

    /**
     * @param args the command line arguments
     */
    public void refresh() {
        try {
            List<TU> tu = (List<TU>) TUkontrol.getKoneksi().getAll();
            TUDataSiswaTabelModel model = new TUDataSiswaTabelModel(tu);
            TabelSiswa.setModel(model);
        } catch (Exception e) {
            Logger.getLogger(TUFormDataSiswa.class.getName()).log(Level.SEVERE, null, e);
        }
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TUFormDataSiswa().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem Delete;
    private javax.swing.JButton HapusButton;
    private javax.swing.JButton KeluarButton;
    private javax.swing.JTable TabelSiswa;
    private javax.swing.JButton TambahButton;
    private javax.swing.JButton UbahButton;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPopupMenu jPopupMenu1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton refreshbutton;
    // End of variables declaration//GEN-END:variables
}
